package com.cn.main.util;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.cn.main.exception.DataException;
import com.cn.main.extract.TaskConf;
import com.cn.main.extract.TaskNode;

public class XmlHelper {

	private static final Logger LOGGER = Logger.getLogger(XmlHelper.class);
	
	public static List<TaskNode> loadTaskJob(String filename){
		
		Document document = null;
		SAXReader saxReader = new SAXReader();       
		try {
			document = saxReader.read(new File(filename));
		} catch (DocumentException e) {
			//e.printStackTrace();
			LOGGER.error(String.format("read[ %s ] error [ %s ]", filename, e.getMessage()));
			throw new DataException(e.getMessage());
		}
		String xpath = "/job/task";
		List<Element> taskEs = (List<Element>) document.selectNodes(xpath);
		List<TaskNode> taskList = new ArrayList<TaskNode>();
		for (Element element : taskEs) {
			if(existsNodeId(taskList, element.attributeValue("id"))){
				throw new DataException("task id [" + element.attributeValue("id") + "] is exists.");
			}
			taskList.add(getTaskNode(element));
		}
		return taskList;
	}
	public static boolean existsNodeId(List<TaskNode> taskList, String id){
		if(StringUtils.isBlank(id)){
			throw new IllegalArgumentException("id is null.");
		}
		boolean rel = false;
		for (TaskNode taskNode : taskList) {
			if(id.trim().equals(taskNode.getId().trim())){
				rel = true;
				break;
			}
		}
		return rel;
	}
	public static TaskNode getTaskNode(Element taskE){
		TaskNode taskNode = new TaskNode();
		taskNode.setId(taskE.attributeValue("id"));
		taskNode.setNextTask(taskE.attributeValue("next_task"));
		//System.out.println(taskNode.getId()+"  "+taskNode.getNextTask());
		
		Element readerE = (Element) taskE.selectSingleNode("reader");
		if (null == readerE){
			throw new DataException("reader node is null!");
		}
		Element readerType = (Element) readerE.selectSingleNode("type");
		TaskConf readerConf = new TaskConf();
		readerConf.setType(readerType.getStringValue().trim());
		Element readerClass = (Element) readerE.selectSingleNode("class");
		readerConf.setClassz(StringUtils.isNotBlank(readerClass.getStringValue())?readerClass.getStringValue():"");
		
		List<Element> readerParam = (List<Element>) readerE.selectNodes("param");
		Map<String, String> readerParamMap = new HashMap<String, String>();
		for (Element element : readerParam) {
			readerParamMap.put(element.attributeValue("key"),element.attributeValue("value").trim());
		}
		readerConf.setParamKeyValues(readerParamMap);
		taskNode.setReaderConf(readerConf);
		
		Element writerE = (Element) taskE.selectSingleNode("writer");
		if (null == writerE){
			throw new DataException("writer node is null!");
		}
		Element writerType = (Element) writerE.selectSingleNode("type");
		TaskConf writerConf = new TaskConf();
		writerConf.setType(writerType.getStringValue().trim());
		Element writerClass = (Element) writerE.selectSingleNode("class");
		writerConf.setClassz(StringUtils.isNotBlank(writerClass.getStringValue())?writerClass.getStringValue():"");
		
		List<Element> writerParam = (List<Element>) readerE.selectNodes("param");
		Map<String, String> writerParamMap = new HashMap<String, String>();
		for (Element element : writerParam) {
			writerParamMap.put(element.attributeValue("key"),element.attributeValue("value").trim());
		}
		writerConf.setParamKeyValues(writerParamMap);
		taskNode.setWriterConf(writerConf);
		return taskNode;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		XmlHelper.loadTaskJob("conf/MysqlToMysql.xml");
	}

}
